package com.helloandroid.paso.ui.multimedia;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.xml.sax.SAXException;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Xml;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.helloandroid.paso.R;
import com.helloandroid.paso.ui.diszko.ImageAdapter;
import com.helloandroid.paso.utils.Logger;
import com.helloandroid.paso.utils.RssItem;
import com.helloandroid.paso.utils.RssReader;
import com.helloandroid.paso.utils.RssReader.RssConnector;

public class Multimedia {// extends Activity {
	
	private ArrayList<HashMap<String, String>> mHashMapListForListView = new ArrayList<HashMap<String, String>>();

	private final static String[] IMAGE_NAMES = { "SZR_110306_9209", "SZR_110306_9210", "SZR_110306_9211", "SZR_110306_9212", "SZR_110306_9214", "SZR_110306_9216", "SZR_110306_9217",
			"SZR_110306_9218", "SZR_110306_9219", "SZR_110306_9222", "SZR_110306_9223", "SZR_110306_9224", "SZR_110306_9226", "SZR_110306_9227" };

	/*
	 * , "SZR_110306_9228", "SZR_110306_9229", "SZR_110306_9230",
	 * "SZR_110306_9231", "SZR_110306_9232", "SZR_110306_9236",
	 * "SZR_110306_9237", "SZR_110306_9238", "SZR_110306_9239",
	 * "SZR_110306_9241", "SZR_110306_9242", "SZR_110306_9244",
	 * "SZR_110306_9249", "SZR_110306_9250", "SZR_110306_9252",
	 * "SZR_110306_9253", "SZR_110306_9254", "SZR_110306_9258",
	 * "SZR_110306_9259", "SZR_110306_9260", "SZR_110306_9261",
	 * "SZR_110306_9262", "SZR_110306_9263", "SZR_110306_9264",
	 * "SZR_110306_9265", "SZR_110306_9266", "SZR_110306_9267",
	 * "SZR_110306_9270", "SZR_110306_9271", "SZR_110306_9272",
	 * "SZR_110306_9273", "SZR_110306_9274", "SZR_110306_9275",
	 * "SZR_110306_9276", "SZR_110306_9277", "SZR_110306_9278",
	 * "SZR_110306_9279", "SZR_110306_9280", "SZR_110306_9281",
	 * "SZR_110306_9282", "SZR_110306_9284", "SZR_110306_9285",
	 * "SZR_110306_9286", "SZR_110306_9287", "SZR_110306_9289",
	 * "SZR_110306_9290", "SZR_110306_9291", "SZR_110306_9292",
	 * "SZR_110306_9293", "SZR_110306_9294", "SZR_110306_9295",
	 * "SZR_110306_9296", "SZR_110306_9297", "SZR_110306_9300",
	 * "SZR_110306_9301", "SZR_110306_9303", "SZR_110306_9304",
	 * "SZR_110306_9305", "SZR_110306_9306", "SZR_110306_9308",
	 * "SZR_110306_9309", "SZR_110306_9310", "SZR_110306_9312",
	 * "SZR_110306_9313", "SZR_110306_9314", "SZR_110306_9316",
	 * "SZR_110306_9317", "SZR_110306_9318", "SZR_110306_9319",
	 * "SZR_110306_9320", "SZR_110306_9322", "SZR_110306_9323",
	 * "SZR_110306_9324", "SZR_110306_9325", "SZR_110306_9326",
	 * "SZR_110306_9327", "SZR_110306_9328", "SZR_110306_9329",
	 * "SZR_110306_9330", "SZR_110306_9331", "SZR_110306_9332",
	 * "SZR_110306_9333", "SZR_110306_9334", "SZR_110306_9335",
	 * "SZR_110306_9336", "SZR_110306_9337", "SZR_110306_9338",
	 * "SZR_110306_9339", "SZR_110306_9340", "SZR_110306_9341",
	 * "SZR_110306_9342", "SZR_110306_9343", "SZR_110306_9350",
	 * "SZR_110306_9351", "SZR_110306_9352", "SZR_110306_9353",
	 * "SZR_110306_9354", "SZR_110306_9355", "SZR_110306_9356",
	 * "SZR_110306_9361", "SZR_110306_9362", "SZR_110306_9363",
	 * "SZR_110306_9364", "SZR_110306_9365", "SZR_110306_9366",
	 * "SZR_110306_9367", "SZR_110306_9368", "SZR_110306_9369",
	 * "SZR_110306_9371", "SZR_110306_9372", "SZR_110306_9373",
	 * "SZR_110306_9374", "SZR_110306_9375", "SZR_110306_9376",
	 * "SZR_110306_9377", "SZR_110306_9378", "SZR_110306_9379",
	 * "SZR_110306_9380", "SZR_110306_9381", "SZR_110306_9382",
	 * "SZR_110306_9383", "SZR_110306_9385", "SZR_110306_9386",
	 * "SZR_110306_9387", "SZR_110306_9388", "SZR_110306_9389",
	 * "SZR_110306_9390", "SZR_110306_9391", "SZR_110306_9392",
	 * "SZR_110306_9393", "SZR_110306_9394", "SZR_110306_9395",
	 * "SZR_110306_9396", "SZR_110306_9397", "SZR_110306_9399",
	 * "SZR_110306_9401", "SZR_110306_9402", "SZR_110306_9403",
	 * "SZR_110306_9404", "SZR_110306_9405", "SZR_110306_9406",
	 * "SZR_110306_9407", "SZR_110306_9409", "SZR_110306_9410",
	 * "SZR_110306_9411", "SZR_110306_9412", "SZR_110306_9413",
	 * "SZR_110306_9414", "SZR_110306_9415", "SZR_110306_9416",
	 * "SZR_110306_9418", "SZR_110306_9419", "SZR_110306_9420",
	 * "SZR_110306_9422", "SZR_110306_9423", "SZR_110306_9424",
	 * "SZR_110306_9425", "SZR_110306_9426", "SZR_110306_9427",
	 * "SZR_110306_9428", "SZR_110306_9429", "SZR_110306_9430",
	 * "SZR_110306_9431", "SZR_110306_9432", "SZR_110306_9433",
	 * "SZR_110306_9434", "SZR_110306_9435", "SZR_110306_9436",
	 * "SZR_110306_9439", "SZR_110306_9440", "SZR_110306_9441",
	 * "SZR_110306_9443", "SZR_110306_9445", "SZR_110306_9446",
	 * "SZR_110306_9447", "SZR_110306_9448", "SZR_110306_9449",
	 * "SZR_110306_9450", "SZR_110306_9451", "SZR_110306_9452",
	 * "SZR_110306_9454", "SZR_110306_9457", "SZR_110306_9458",
	 * "SZR_110306_9460", "SZR_110306_9461", "SZR_110306_9463",
	 * "SZR_110306_9464", "SZR_110306_9465", "SZR_110306_9466",
	 * "SZR_110306_9467", "SZR_110306_9468", "SZR_110306_9469",
	 * "SZR_110306_9470", "SZR_110306_9471", "SZR_110306_9474",
	 * "SZR_110306_9475", "SZR_110306_9476", "SZR_110306_9477",
	 * "SZR_110306_9478", "SZR_110306_9479", "SZR_110306_9481",
	 * "SZR_110306_9482", "SZR_110306_9483", "SZR_110306_9484",
	 * "SZR_110306_9485", "SZR_110306_9486", "SZR_110306_9487",
	 * "SZR_110306_9488", "SZR_110306_9490", "SZR_110306_9491",
	 * "SZR_110306_9492", "SZR_110306_9493", "SZR_110306_9494",
	 * "SZR_110306_9495", "SZR_110306_9498", "SZR_110306_9499",
	 * "SZR_110306_9500"};
	 */
	/*
	 * pl
	 * http://koncertfoto.hu/koncertfoto.hu/2011-03/Pages/PASO_%40_Godor_files
	 * /Media/SZR_110306_9209/thumb.jpg
	 * http://koncertfoto.hu/koncertfoto.hu/2011
	 * -03/Pages/PASO_%40_Godor_files/Media/SZR_110306_9209/SZR_110306_9209.jpg
	 * http
	 * ://koncertfoto.hu/koncertfoto.hu/2011-03/Pages/PASO_%40_Godor_files/rss
	 * .xml
	 */
	private class RSSAsyncTask extends AsyncTask<String, Void, Void> {

		ArrayList<RssItem> articles = new ArrayList<RssItem>();

		@Override
		protected Void doInBackground(String... arg0) {
			RssConnector rss = new RssReader().new RssConnector(Xml.Encoding.UTF_8);
			try {
				articles = (ArrayList<RssItem>) rss.getRssItems(new URL(
						"http://gdata.youtube.com/feeds/base/users/pannoniaallstars/uploads?alt=rss&amp;v=2&amp;orderby=published&amp;client=ytapi-youtube-profile"));
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {

			int size = articles.size();
			for (int i = 0; i < size; i++) {
				HashMap<String, String> currentHashmap = new HashMap<String, String>();
				currentHashmap.put("link", articles.get(i).mLink);
				currentHashmap.put("title", articles.get(i).mTitle);
				mHashMapListForListView.add(currentHashmap);
				Logger.log(currentHashmap.toString());
			}
			mSpecialAdapter.notifyDataSetChanged();
			mProgressBar.setVisibility(ProgressBar.GONE);
			super.onPostExecute(result);

			// TODO load values into listview
			// put items into hasmap of the adapter
		}
	}

	SpecialAdapterVideolist mSpecialAdapter;
	ProgressBar mProgressBar;

	
	
	/**
	 * getMultimedia((WebView)findViewById(R.id.multimedia_wView),
	 * (ListView)findViewById(R.id.multimedia_listView1),
	 * (Button)findViewById(R.id.multimedia_button1),
	 * (Button)findViewById(R.id.multimedia_button2), getApplicationContext(),
	 * (ProgressBar)findViewById(R.id.multimedia_progressBar1));
	 * 
	 * @param galleryItemWebview
	 * @param videoListView
	 * @param galleryScreenButton
	 * @param videoScreenButton
	 * @param context
	 * @param webview
	 * @param picgallery
	 * @param res
	 * @param url_addres
	 */
	public void getMultimedia(final ListView videoListView, Button galleryScreenButton, Button videoScreenButton, final Context context, ProgressBar pg,
			final WebView webview, final Gallery picgallery, final Resources res, final int url_addres) {
		mProgressBar = pg;
		webview.setVisibility(WebView.GONE);
		picgallery.setVisibility(Gallery.GONE);
		
		/**
		 * Videolist:
		 */
		
		new RSSAsyncTask().execute();

		galleryScreenButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				webview.setVisibility(WebView.VISIBLE);
				videoListView.setVisibility(ListView.GONE);
				picgallery.setVisibility(Gallery.VISIBLE);
			}
		});

		videoScreenButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				webview.setVisibility(WebView.GONE);
				videoListView.setVisibility(ListView.VISIBLE);
				picgallery.setVisibility(Gallery.GONE);
			}
		});

		videoListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Intent ibrowser = new Intent(Intent.ACTION_VIEW, Uri.parse(mHashMapListForListView.get(arg2).get("link")));
				ibrowser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(ibrowser);
			}
		});

		mSpecialAdapter = new SpecialAdapterVideolist(context, mHashMapListForListView, R.layout.multimedialistviewitem, new String[] { "title" }, new int[] { R.id.multimedialistviewitem_textView });

		videoListView.setAdapter(mSpecialAdapter); // adapterForList

		// multiWebView
		// .loadUrl("http://koncertfoto.hu/koncertfoto.hu/2011-03/Pages/PASO_%40_Godor_files/Media/"
		// + IMAGE_NAMES[0] + "/" + IMAGE_NAMES[0] + ".jpg");

		/**
		 * iamge gallery:
		 */
		
		// default htm page
		// discoWebView = (WebView) findViewById(R.id.diszko_ui_screen_webview);
		webview.getSettings().setJavaScriptEnabled(true);
		webview.loadUrl("http://paso.hu/diszko/imtheone.htm");

		// gallery declarations
		// Gallery g = (Gallery) findViewById(R.id.diszko_ui_screen_galery);
		Gallery g = picgallery;
		g.setAdapter(new ImageAdapter(context));

		g.setOnItemClickListener(new OnItemClickListener() {
			// Resources res = getResources();
			String[] url = res.getStringArray(url_addres);

			public void onItemClick(AdapterView parent, View v, int position, long id) {
				// no result, album and other check
				webview.getSettings().setJavaScriptEnabled(true);
				webview.loadUrl(url[position]);
			}
		});
	}
}